let obj = new URLSearchParams(location.search);
console.log(obj);
let pid = obj.get("id");
console.log(pid);

let main = document.querySelector(".main");
console.log(main);
axios.get(productDeailAPI,{params:{id:pid}})
     .then((res)=>{
         console.log(res.data);
         let pObj = res.data.data;
         console.log(pObj);
         let html =
          `
          <div class="product-info">
          <div class="info-left">
              <div class="face-box">
                  <img src="${pObj.pimg}" alt="">
              </div>
          </div>
          <div class="info-right">
              <p class="title">${pObj.pname}</p>
              <div class="price-box">
                  <div class="price-box-left">
                      <i>￥</i>
                      <span class="now-price">${pObj.pprice}</span>
                  </div>
                  <div class="price-box-right">
                      <p class="sales">
                          交易成功:
                          <i>236</i>
                      </p>
                  </div>
              </div>
              <div class="stock-box">
                     <div class="buycount">
                         <span class="tit">数量</span>
                         <div class="stock">
                             <span class="add">+</span>
                             <input type="text"  class="num" value="1" min="1">
                             <span class="sum">-</span>
                             <i class="tet">只限制3件</i>
                         </div> 
                     </div>
                 </div>
                 <div class="buybtn">
                     <a href="" class="gobuy">立即剁手</a>
                     <div class="addcart">
                         <i></i>
                         <span>加入购物车</span>
                     </div>
                 </div>
         `;
         main.innerHTML = html;
         //  给购物车按钮添加事件
        let addCart = document.querySelector(".addcart");
        console.log(addCart);
        addCart.onclick = async function(){
            console.log(111);
        let uid = localStorage.getItem("uid");
        let num = document.querySelector(".num");
        let pnum = num.value;
        
        let {data} = await axios.get(CartAddAPI,{
            params:{uid,pid,pnum},
        });
        console.log(data);
        alert("加入购物车成功");
    }

        // 加入数量
        pagenumFn()
        function pagenumFn(){
        // let add = document.querySelector(".add");
        // let num = document.querySelector(".num");
        // let sum = document.querySelector(".sum");

        // // let pagenum = num.value;
        
        // add.onclick = function(){
        //     console.log(222);
        //     let pagenum = num.value;
        //     // console.log(pagenum);
        //     pagenum++;
        //     if(pagenum > 10){
        //         return;
        //     }
        //    num.value = pagenum;
        //     // num.innerText = pagenum;
        // }

        // sum.onclick = function(){
        //     console.log(333);
        //     let pagenum = num.value;
        //     pagenum--;
        //     if(pagenum < 1){
        //         return
        //     }
        //     num.value = pagenum;
        // }

        // num.onchange = function(){
        //     let tet = document.querySelector(".tet")
        //     console.log(444);
        //     let pagenum = num.value;
        //     if(pagenum > 10){
        //         pagenum = 10;
        //         tet.style.display = "block"
        //     }
        //     num.value = pagenum;
        // }
        $(".add").click(function(){
            // window.location.reload();
            // console.log(222);
            let pnum = $(this).siblings(".num").val();
    
            pnum++;
            if(pnum>3){
                pnum=3;
                $(".tet").css("display","block");
            }
            // else if(pnum<3){
            //     $(".tet").css("display","none");
            // }
            $(this).siblings(".num").val(pnum);
            console.log(pnum);
            let pid = $(this).attr("data-pid");
            let uid = localStorage.getItem("uid");
            axios 
                .get(cartUpdateAPI,{params:{uid,pid,pnum}})
                .then((res)=>{
                    console.log(res.data);
                })
        })
    
        $(".sum").click(function(){
            // location.reload();
            // console.log(333);
            let pnum = $(this).siblings(".num").val();
    
            pnum--;
            if(pnum<1){
                pnum = 1;
            } else if(pnum<3){
                $(".tet").css("display","none");
            }
            $(this).siblings(".num").val(pnum);
            let pid = $(this).attr("data-pid");
            let uid = localStorage.getItem("uid");
            axios 
                .get(cartUpdateAPI,{params:{uid,pid,pnum}})
                .then((res)=>{
                    console.log(res.data);
                })
                
        })
    
        $(".num").change(function(){
            console.log(123);
            let pnum = parseInt($(this).val());
            if(pnum>3){
                pnum = 3;
                $(".tet").css("display","block");
            }else if(pnum<=1){
                pnum = 1;
            }else if(typeof pnum != "number" || isNaN(pnum)){
                pnum = 1;
            }
    
            $(this).val(pnum);
            let pid = $(this).attr("data-pid");
            let uid = localStorage.getItem("uid");
            axios 
                .get(cartUpdateAPI,{params:{uid,pid,pnum}})
                .then((res)=>{
                    console.log(res.data);
                })
        })
        // countPrice()
    }

       })

// })

    